package javolution.context;

import javax.realtime.MemoryArea;
import javolution.context.LocalContext;
import javolution.lang.Configurable;
import javolution.lang.MathLib;
import javolution.lang.Reflection;

/* loaded from: classes2.dex */
public abstract class ConcurrentContext extends Context {
    public static final Configurable<Class<? extends ConcurrentContext>> DEFAULT = new Configurable<>(Default.CLASS);
    public static final Configurable<Integer> MAXIMUM_CONCURRENCY = new Configurable(new Integer(availableProcessors() - 1)) { // from class: javolution.context.ConcurrentContext.1
        @Override // javolution.lang.Configurable
        protected void notifyChange() {
            ConcurrentContext.CONCURRENCY.setDefault(get());
        }
    };
    private static final LocalContext.Reference CONCURRENCY = new LocalContext.Reference(MAXIMUM_CONCURRENCY.get());

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes2.dex */
    public static final class Default extends ConcurrentContext {
        private int _completed;
        private int _concurrency;
        private Throwable _error;
        private int _initiated;
        private static final Class CLASS = new Default().getClass();
        private static ConcurrentThread[] _Executors = new ConcurrentThread[0];
        private static final Runnable CREATE_EXECUTORS = new Runnable() { // from class: javolution.context.ConcurrentContext.Default.1
            @Override // java.lang.Runnable
            public synchronized void run() {
                int intValue = ConcurrentContext.MAXIMUM_CONCURRENCY.get().intValue();
                int length = Default._Executors.length;
                if (length >= intValue) {
                    return;
                }
                ConcurrentThread[] concurrentThreadArr = new ConcurrentThread[intValue];
                System.arraycopy(Default._Executors, 0, concurrentThreadArr, 0, length);
                while (length < intValue) {
                    concurrentThreadArr[length] = new ConcurrentThread();
                    concurrentThreadArr[length].start();
                    length++;
                }
                ConcurrentThread[] unused = Default._Executors = concurrentThreadArr;
            }
        };

        Default() {
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void completed() {
            synchronized (this) {
                this._completed++;
                notify();
            }
            AllocatorContext.getCurrent().deactivate();
        }

        @Override // javolution.context.Context
        protected void enterAction() {
            this._error = null;
            this._initiated = 0;
            this._completed = 0;
            this._concurrency = ConcurrentContext.getConcurrency();
            int i = this._concurrency;
            ConcurrentThread[] concurrentThreadArr = _Executors;
            if (i > concurrentThreadArr.length) {
                MemoryArea.getMemoryArea(concurrentThreadArr).executeInArea(CREATE_EXECUTORS);
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void error(Throwable th) {
            synchronized (this) {
                if (this._error == null) {
                    this._error = th;
                }
            }
        }

        @Override // javolution.context.ConcurrentContext
        protected void executeAction(Runnable runnable) {
            int i = this._concurrency;
            do {
                i--;
                if (i < 0) {
                    runnable.run();
                    return;
                }
            } while (!_Executors[i].execute(runnable, this));
            this._initiated++;
        }

        @Override // javolution.context.Context
        protected void exitAction() {
            synchronized (this) {
                while (this._initiated != this._completed) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new ConcurrentException(e);
                    }
                }
            }
            Throwable th = this._error;
            if (th != null) {
                if (th instanceof RuntimeException) {
                    throw ((RuntimeException) th);
                }
                if (!(th instanceof Error)) {
                    throw new ConcurrentException(th);
                }
                throw ((Error) th);
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void started() {
            Context.setCurrent(this);
        }
    }

    static {
        ObjectFactory.setInstance(new ObjectFactory() { // from class: javolution.context.ConcurrentContext.2
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new Default();
            }
        }, Default.CLASS);
    }

    protected ConcurrentContext() {
    }

    private static int availableProcessors() {
        Reflection.Method method = Reflection.getMethod("java.lang.Runtime.availableProcessors()");
        if (method != null) {
            return ((Integer) method.invoke(Runtime.getRuntime())).intValue();
        }
        return 1;
    }

    public static ConcurrentContext enter() {
        return (ConcurrentContext) Context.enter((Class<Context>) DEFAULT.get());
    }

    public static void execute(Runnable runnable) {
        ((ConcurrentContext) Context.getCurrent()).executeAction(runnable);
    }

    public static ConcurrentContext exit() {
        return (ConcurrentContext) Context.exit();
    }

    public static int getConcurrency() {
        return ((Integer) CONCURRENCY.get()).intValue();
    }

    public static void setConcurrency(int i) {
        CONCURRENCY.set(new Integer(MathLib.min(MAXIMUM_CONCURRENCY.get().intValue(), MathLib.max(0, i))));
    }

    protected abstract void executeAction(Runnable runnable);
}
